import axios from "axios";

const request = axios.create({
  baseURL: "http://localhost:5173",
  timeout: 5000,
});

// 请求拦截
request.interceptors.request.use(
  (config) => {
    // 获得 token -> 在请求头加入token
    const token = localStorage.getItem("token");
    if (token) {
      config.headers["Authorization"] = "Bearer " + token;
    }

    // 请求放行
    return config;
  },
  (err) => {
    console.log("请求拦截失败，错误信息：", err);
  }
);

// 响应拦截
request.interceptors.response.use(
  (response) => {
    const res = response.data;
    return res;
  },
  (err) => {
    console.log("响应拦截失败，错误信息：", err);
  }
);

export default request;
